﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using OT.Data;
using OT.Areas.Admin.Models;

namespace OT.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            try
            {
                int UserID = 0;
               
                    if (Request.Cookies["AdminID"] != null)
                    {
                        UserID =Convert.ToInt16( Request.Cookies["AdminID"].Value); 
                    }
                     else
                    {
                    TempData["notice"] = "Time Out ,Please Login";
                    Response.Redirect("~/Home");
                    }
                if (ModelState.IsValid)
                {
                    var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                    if (User != null)
                    {
                        User.LastLoginDate = DateTime.Now;
                        OTDb.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View();
        }
        #endregion

        #region Logout
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            HttpCookie myCookie = new HttpCookie("Admin");
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
            return RedirectToAction("Index", "Login");
        }
        #endregion

        #region Extracallfunction
        public JsonResult Test(string fillColor)
        {
            var datasets = new
                             {
                                 data = OTDb.Categories.Select(x => x.OrderNumber).ToList()
                             };
            return Json(new
          {
              datasets,
              labels = OTDb.Subjects.Select(x => x.SubjectName).ToList()
          });
        }

        #endregion
    }
}
